﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MyUtility;
using MyAuction;

namespace MyAdmin.Admin
{
    public partial class Ad_MemberLog1 : System.Web.UI.Page
    {
        public GetRole mGetRole;
        public int PageIndex = 1;

        MemberLog mMemberLog = new MemberLog();


        private bool CheckPermission()
        {
            try
            {
                if (mGetRole.ViewRole == false)
                {
                    Response.Redirect(mGetRole.URLNotView, false);
                    return false;
                }
                if (Member.LoginName().ToLower() == "chilinh")
                {
                    lbtn_Delete.Visible = mGetRole.DeleteRole;
                }
            }
            catch (Exception ex)
            {
                 MyLogfile.WriteLogError(ex, true, MyNotice.AdminError.CheckPermissionError, "Chilinh");
            }
            return true;
        }


        private void BindData()
        {
            Admin_Paging1.ResetLoadData();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                //Phân quyền
                if (ViewState["Role"] == null)
                {
                    mGetRole = new GetRole(LocalConfig.ListPage.MemberLog, Member.MemberGroupID());
                }
                else
                {
                    mGetRole = (GetRole)ViewState["Role"];
                }
                if (!CheckPermission())
                    return;

                Admin_Paging1.rpt_Data = rpt_Data;
                Admin_Paging1.GetData_Callback_Change += new MyAdmin.Admin_Control.Admin_Paging.GetData_Callback(Admin_Paging1_GetData_Callback_Change);
                Admin_Paging1.GetTotalPage_Callback_Change += new MyAdmin.Admin_Control.Admin_Paging.GetTotalPage_Callback(Admin_Paging1_GetTotalPage_Callback_Change);
            }
            catch (Exception ex)
            {
                MyLogfile.WriteLogError(ex, true, MyNotice.AdminError.LoadDataError, "Chilinh");
            }
        }

        int Admin_Paging1_GetTotalPage_Callback_Change()
        {
            try
            {
                string str_SearchContent = "";
                bool? IsSuccess = null;
                string SortBy = null;
                int SearchType = 0;

                if (btn_CancelSearch.Visible)
                {
                    SearchType = int.Parse(sel_SearchType.Value);

                    str_SearchContent = tbx_Search.Value.Length < 1 ? null : tbx_Search.Value;
                    SortBy = sel_Sort.SelectedIndex > 0 ? sel_Sort.Value : null;
                    if (rad_Success.Checked)
                        IsSuccess = true;
                    if (rad_UnSuccess.Checked)
                        IsSuccess = false;
                }
                return mMemberLog.Select_TotalRow(SearchType, str_SearchContent, IsSuccess);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        DataTable Admin_Paging1_GetData_Callback_Change()
        {
            try
            {
                string str_SearchContent = "";
                bool? IsSuccess = null;
                string SortBy = null;
                int SearchType = 0;

                if (btn_CancelSearch.Visible)
                {
                    SearchType = int.Parse(sel_SearchType.Value);

                    str_SearchContent = tbx_Search.Value.Length < 1 ? null : tbx_Search.Value;
                    SortBy = sel_Sort.SelectedIndex > 0 ? sel_Sort.Value : null;
                    if (rad_Success.Checked)
                        IsSuccess = true;
                    if (rad_UnSuccess.Checked)
                        IsSuccess = false;
                }

                PageIndex = (Admin_Paging1.mPaging.CurrentPageIndex - 1) * Admin_Paging1.mPaging.PageSize + 1;

                return mMemberLog.Search(SearchType, Admin_Paging1.mPaging.BeginRow, Admin_Paging1.mPaging.EndRow,
                                       str_SearchContent, IsSuccess, SortBy);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        protected void lbtn_Delete_Click(object sender, EventArgs e)
        {
            try
            {
                char[] key_1 = { '|' };

                string[] arr_1 = hid_ListCheckAll.Value.Split(key_1);


                DataSet dds_Parent = new DataSet("Parent");
                DataTable tbl_Child = new DataTable("Child");
                DataColumn col_1 = new DataColumn("ID", typeof(int));
                tbl_Child.Columns.Add(col_1);

                for (int i = 0; i < arr_1.Length; i++)
                {
                    DataRow mRow = tbl_Child.NewRow();

                    mRow["ID"] = int.Parse(arr_1[i]);

                    tbl_Child.Rows.Add(mRow);
                }
                tbl_Child.AcceptChanges();

                dds_Parent.Tables.Add(tbl_Child);
                dds_Parent.AcceptChanges();

                if (mMemberLog.Delete(0, dds_Parent.GetXml()))
                {
                    #region Log member
                    MemberLog mLog = new MemberLog();
                    MemberLog.ActionType Action = MemberLog.ActionType.Delete;

                    mLog.Insert_Success("Audio", hid_ListCheckAll.Value, string.Empty, Action, true);

                    #endregion

                    MyMessage.ShowMessage("Xóa dữ liệu thành công.");
                    BindData();
                }
                else
                {
                    MyMessage.ShowMessage("Xóa dữ liệu KHÔNG thành công!");
                }
            }
            catch (Exception ex)
            {
                MyLogfile.WriteLogError(ex, true, MyNotice.AdminError.DeleteDataError, "Chilinh");
            }
        }

        protected void btn_Search_Click(object sender, EventArgs e)
        {
            try
            {
                btn_CancelSearch.Visible = true;
                BindData();
            }
            catch (Exception ex)
            {
                MyLogfile.WriteLogError(ex, true, MyNotice.AdminError.SeachError, "Chilinh");
            }
        }

        protected void btn_CancelSearch_Click(object sender, EventArgs e)
        {
            try
            {
                btn_CancelSearch.Visible = false;
                BindData();
            }
            catch (Exception ex)
            {
                MyLogfile.WriteLogError(ex, true, MyNotice.AdminError.SeachError, "Chilinh");
            }
        }

    }
}
